<?php

class Application_Model_EventoCategory extends Zend_Db_Table_Abstract
{
    protected $_name = 'eventoCategory';
    protected $_primary = array('evento_evId', 'category_catId');
    protected $_referenceMap = array(
        'Categorys' => array(
            'columns'       => array('category_catId'),
            'refTableClass' => 'Application_Model_Categorys',
            'refColumns'    => 'catId',
        ),
        'Eventos' => array(
            'columns'       => array('evento_evId'),
            'refTableClass' => 'Application_Model_Eventos',
            'refColumns'    => 'evId',
        )
    );
    
    /**
     * Create a eventoCtategory record
     *
     * @param int $evId
     * @param int $catId 
     */
    public function createEventoCategory($evId, $catId)
    {
        $rowEventoCategory = $this->createRow();
        if ($rowEventoCategory) {
            $rowEventoCategory->evento_evId = $evId;
            $rowEventoCategory->category_catId = $catId;
            $rowEventoCategory->save();
        } else {
            throw new Zend_Exception("Non è possibile inserire la categoria!");
        }
    }
    
    public function updateEventoCategory($evId, $catId)
    {
        $rowEventoCategory = $this->find($evId, $catId)->current();
        if ($rowEventoCategory) {
            $rowEventoCategory->evento_evId = $evId;
            $rowEventoCategory->category_catId = $catId;
            $rowEventoCategory->save();
        } else {
            $this->createEventoCategory($evId, $catId);
        }
    }
    
    public function deleteEventoCategory($evId, $catId) 
    {
        $rowEventoCategory = $this->find($evId, $catId)->current();
        if ($rowEventoCategory) {
            $rowEventoCategory->delete();
        } else {
            throw new Zend_Exception("Impossibile elimibare la categoria");
        }
    }
}

